<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const promise = new Promise((resolve, reject) => {
      resolve("aaaaa")
    })

    // Promise本身就支持链式调用
    // then方法是返回一个新的Promise,链式中的then是在等待这个新的Promise有决议之后执行的
    promise.then(res => {
      console.log('第一个then:', res);
      return 'bbb'
    }).then(res => {
      console.log('第二个then:', res);
    }).then(res => {
      console.log('第三个then:', res);
    }).then(res => {
      console.log('第四个then:', res);
    })
    promise.then(res => {
      console.log('添加第二个then方法');
    })

    // 2.then方法传入回调函数的返回值类型
    const newPromise = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve("why")
      }, 3000)
    })

    promise.then(res => {
      console.log("1.第一个Promise的then方法:", res)
      // 1.普通值
      return "bbbbbbb"
    }).then(res => {
      console.log("1.第二个Promise的then方法:", res) // undefined
    })

    promise.then(res => {
      console.log("2.第一个Promise的then方法:", res)
      // 2.新的Promise
      return newPromise
    }).then(res => {
      console.log("2.第二个Promise的then方法:", res) // undefined
    })

    promise.then(res => {
      console.log("3.第一个Promise的then方法:", res)
      // 3.thenable的对象
      return {
        then: function(resolve) {
          resolve("thenable")
        }
      }
    }).then(res => {
      console.log("3.第二个Promise的then方法:", res) // undefined
    })
  </script>
</body>

</html>